﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cleverscape.UTorrentClient.WebClient;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using uTorrentNetClient.ModelDesign.Contracts;
using uTorrentNetClient.ModelDesign.Events;
using uTorrentNetClient.ModelDesign.Helper;

namespace uTorrentNetClient.ModelDesign.TorrentProperties.Model
{
    public class PropertiesModel : Base, IPropertiesModel
    {
        private readonly IEventAggregator _eventAggregator;

        public IPropertiesView View { get; set; }

        public TorrentDecorator Torrent { get; set; }
        private TorrentFileDecorator _selectedFile;
        public TorrentFileDecorator SelectedFile
        {
            get { return _selectedFile; }
            set
            {
                _selectedFile = value;
                SetNotifyPropertyChanged("SelectedFile");
            }
        }

        public DelegateCommand<object> OkCommand { get; set; }
        public DelegateCommand<object> CancelCommand { get; set; }
        public DelegateCommand<object> SetFileNoDownloadCommand { get; set; }
        public DelegateCommand<object> SetFileNormalCommand { get; set; }
        public DelegateCommand<object> SetFileHighCommand { get; set; }
        public DelegateCommand<object> SetFileLowCommand { get; set; }

        public PropertiesModel(IPropertiesView view, IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            View = view;
            View.DataContext = this;

            OkCommand = new DelegateCommand<object>(okCommandExecute);
            CancelCommand = new DelegateCommand<object>(cancelCommandExecute);
            SetFileNoDownloadCommand = new DelegateCommand<object>(fileNoDownloadCommandExcecute);
            SetFileNormalCommand = new DelegateCommand<object>(fileNormalCommandExcecute);
            SetFileHighCommand = new DelegateCommand<object>(fileHighCommandExcecute);
            SetFileLowCommand = new DelegateCommand<object>(fileLowCommandExcecute);
        }

        private void fileLowCommandExcecute(object obj)
        {
            SelectedFile.Priority = FilePriority.LowPriority;
        }

        private void fileHighCommandExcecute(object obj)
        {
            SelectedFile.Priority = FilePriority.HighPriority;
        }

        private void fileNormalCommandExcecute(object obj)
        {
            SelectedFile.Priority = FilePriority.NormalPriority;
        }

        private void fileNoDownloadCommandExcecute(object obj)
        {
            SelectedFile.Priority = FilePriority.DontDownload;
        }

        private void cancelCommandExecute(object obj)
        {
            _eventAggregator.GetEvent<ClosePropertiesEvent>().Publish(false);
        }

        private void okCommandExecute(object obj)
        {
            Torrent.SaveChanges();
            _eventAggregator.GetEvent<ClosePropertiesEvent>().Publish(true);
        }
    }
}
